import 'package:flutter/material.dart';

// 引入Toast插件
import 'package:fluttertoast/fluttertoast.dart';

// 引入自定义dialog弹窗
import '../widget/myDialog.dart';

class DialogPage extends StatefulWidget {
  const DialogPage({super.key});

  @override
  State<DialogPage> createState() => _DialogPageState();
}

class _DialogPageState extends State<DialogPage> {
  void _alertDialog() async {
    var result = await showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('提示信息'),
          content: Text('确定要删除吗？'),
          actions: [
            TextButton(
              onPressed: () {
                // print('ok');
                Navigator.of(context).pop('确定'); //关闭弹窗
              },
              child: Text('确定'),
            ),
            TextButton(
              onPressed: () {
                // print('cancel');
                Navigator.of(context).pop('取消');
              },
              child: Text('取消'),
            ),
          ],
        );
      },
    );
    print('----------------');
    print('result:$result');
  }

  void _simpleDialog() async {
    var result = await showDialog(
      barrierDismissible: true, //点击弹窗外部是否关闭弹窗
      context: context,
      builder: (context) {
        return SimpleDialog(
          title: Text('选择语言'),
          children: [
            SimpleDialogOption(
              onPressed: () {
                print('汉语');
                Navigator.of(context).pop('汉语');
              },
              child: Text('汉语'),
            ),
            Divider(),
            SimpleDialogOption(
              onPressed: () {
                print('英语');
                Navigator.pop(context, '英语');
              },
              child: Text('英语'),
            ),
            Divider(),

            SimpleDialogOption(
              onPressed: () {
                print('韩语');
                Navigator.of(context).pop('韩语');
              },
              child: Text('韩语'),
            ),
            Divider(),
          ],
        );
      },
    );

    print('----------------');
    print('result:$result');
  }

  void _showModalBottomSheet() async {
    var result = await showModalBottomSheet(
      context: context,
      builder: (context) {
        return SizedBox(
          height: 200,
          child: Column(
            children: [
              ListTile(
                title: Text('分享'),
                onTap: () {
                  Navigator.pop(context, '分享');
                },
              ),
              Divider(),
              ListTile(
                title: Text('收藏'),
                onTap: () {
                  Navigator.pop(context, '收藏');
                },
              ),
              Divider(),

              ListTile(
                title: Text('取消'),
                onTap: () {
                  Navigator.pop(context, '取消');
                },
              ),
            ],
          ),
        );
      },
    );

    print('----------------');
    print('result:$result');
  }

  void _toast() {
    Fluttertoast.showToast(
      msg: "丁牛爱哞哞哞",
      toastLength: Toast.LENGTH_LONG, //提示时间  针对android
      gravity: ToastGravity.TOP, //方位
      timeInSecForIosWeb: 1, //提示时间  针对ios web
      backgroundColor: Colors.red, //背景颜色
      textColor: Colors.white, //文字颜色
      fontSize: 16.0, //文本字体大小
    );
  }

  void _MyDialog() async{
    var result = await showDialog(
      barrierDismissible: false,
      context: context,
      builder: (context) {
        return Mydialog( 
          title: '玩游戏',
          ctent: '确定要玩吗？',
          closeBtn: () {
            Navigator.pop(context,'关闭成功');
          },
        );
      },
    );

    print('result:$result');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Dialog演示页面')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(onPressed: _alertDialog, child: Text('AlertDialog')),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _simpleDialog,
              child: Text('SimpleDialog'),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: _showModalBottomSheet,
              child: Text('showModalBottomSheet'),
            ),
            SizedBox(height: 20),
            ElevatedButton(onPressed: _toast, child: Text('Toast')),
            SizedBox(height: 20),
            Divider(),
            //自定义dialog弹窗
            OutlinedButton(onPressed: _MyDialog, child: Text('自定义dialog弹窗')),
          ],
        ),
      ),
    );
  }
}
